//
//  DetailViewOfTVCToPlay.m
//  To Feel
//
//  Created by 曹宇 on 16/7/14.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "DetailViewOfTVCToPlay.h"
#import "ModelOfSecondTVC.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
@interface DetailViewOfTVCToPlay ()
@property(nonatomic, retain) UILabel *labelOfTitle;
@property(nonatomic, retain) UIView *viewOfLine;
@property(nonatomic, retain) UILabel *labelOfTime;
@property(nonatomic, retain) UILabel *labelOfInfo;
@property(nonatomic, retain) UIButton *buttonOfLike;
@property(nonatomic, retain) UIButton *buttonOfShare;
@property(nonatomic, retain) UIButton *buttonOfCount;
@property(nonatomic, retain) UIImageView *imgOfPlayButton;
@end

@implementation DetailViewOfTVCToPlay

- (instancetype)initWithFrame:(CGRect)frame model:(ModelOfSecondTVC *)model{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentMode = UIViewContentModeTop;
        self.clipsToBounds = YES;
        self.modelOfData = model;
        [self creatSubview];
    }
    
    return self;
}

- (void)creatSubview {
    
    self.imgOfTop = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.7)];
    
    [self addSubview:_imgOfTop];
    UIImageView *imgViewOfButton = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"播放2"]];
   
    [self addSubview:imgViewOfButton];
    [imgViewOfButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(SCREEN_HEIGHT/3.0 - 30);
        make.left.equalTo(self.mas_left).with.offset(SCREEN_WIDTH/2.0 - 20);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    
    self.imgOfBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 1.7, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_HEIGHT / 1.7)];
    
    [self addSubview:_imgOfBottom];
    
    self.labelOfTitle = [[UILabel alloc] init];
    
    [self.imgOfBottom addSubview:_labelOfTitle];
    
    [self.labelOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgOfBottom).with.offset(13);
        make.leading.equalTo(self.imgOfBottom).with.offset(13);
        make.height.offset(25);
    }];
    
    self.viewOfLine = [[UIView alloc] init];
    
    [self.imgOfBottom addSubview:_viewOfLine];
    
    [self.viewOfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfTitle.mas_bottom).with.offset(13);
        make.leading.equalTo(self.labelOfTitle);
        make.width.offset((self.imgOfBottom.frame.size.width - 26) / 2);
        make.height.offset(0.6);
    }];
    
    self.labelOfTime = [[UILabel alloc] init];
    
    [self.imgOfBottom addSubview:_labelOfTime];
    
    [self.labelOfTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewOfLine.mas_bottom).with.offset(13);
        make.leading.equalTo(self.viewOfLine);
        make.height.offset(18);
    }];
    
    self.labelOfInfo = [[UILabel alloc] init];
    
    [self.imgOfBottom addSubview:_labelOfInfo];
    
    [self.labelOfInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelOfTime.mas_bottom).with.offset(13);
        make.leading.equalTo(self.labelOfTime);
        make.trailing.equalTo(self.imgOfBottom).with.offset(-13);
    }];
    
    NSArray *arr = [_modelOfData.data objectForKey:@"tags"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UILabel *label = [[UILabel alloc] init];
        [self.imgOfBottom addSubview:label];
        
        label.frame = CGRectMake(SCREEN_WIDTH / 8 * i + SCREEN_WIDTH / 4, 64, 40, 18);
        //        [label mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.top.equalTo(self.labelOfTime);
        ////            make.leading.equalTo(self.imgOfBottom).offset((self.labelOfTime.frame.size.width + self.labelOfTime.frame.origin.x + 5) + label.frame.size.width * i);
        //            make.leading.equalTo(self.labelOfTime.mas_trailing).offset(45 * i + 10);
        //            make.height.offset(18);
        //        }];
        label.text = [[[_modelOfData.data objectForKey:@"tags"] objectAtIndex:i] objectForKey:@"name"];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:13];
        label.layer.cornerRadius = 3;
        label.layer.masksToBounds = YES;
        label.backgroundColor = [UIColor colorWithRed:124 / 255.0 green:124 / 255.0 blue:124 / 255.0 alpha:0.4];
        
    }
    
    self.buttonOfLike = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.imgOfBottom addSubview:self.buttonOfLike];
    
    [self.buttonOfLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.labelOfInfo);
        make.bottom.equalTo(self.imgOfBottom).with.offset(-65);
        make.height.offset(18);
    }];
    
    self.buttonOfShare = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.imgOfBottom addSubview:self.buttonOfShare];
    
    [self.buttonOfShare mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.buttonOfLike.mas_trailing).with.offset(50);
        make.bottom.equalTo(self.buttonOfLike);
        make.height.offset(18);
    }];
    
    self.buttonOfCount = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.imgOfBottom addSubview:self.buttonOfCount];
    
    [self.buttonOfCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.buttonOfShare.mas_trailing).with.offset(50);
        make.bottom.equalTo(self.buttonOfShare);
        make.height.offset(18);
    }];
    
    self.imgOfPlayButton = [[UIImageView alloc] init];
    
    [self.imgOfTop addSubview:self.imgOfPlayButton];
    
    [self.imgOfPlayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.imgOfTop);
        make.centerY.equalTo(self.imgOfTop);
        make.width.offset(100);
        make.height.offset(100);
    }];
    
    [self configSubview];
    
}

- (void)configSubview {
    
    self.imgOfTop.contentMode = UIViewContentModeScaleAspectFill;
    self.imgOfTop.layer.masksToBounds = YES;
    
    NSURL *urlOfBottom = [NSURL URLWithString:[[_modelOfData.data objectForKey:@"cover"]objectForKey:@"blurred"]];
    [self.imgOfBottom sd_setImageWithURL:urlOfBottom placeholderImage:nil];
    
    self.labelOfTitle.text = [_modelOfData.data objectForKey:@"title"];
    self.labelOfTitle.textColor = [UIColor whiteColor];
    self.labelOfTitle.font = [UIFont systemFontOfSize:18 weight:1];
    self.viewOfLine.backgroundColor = [UIColor whiteColor];
    
    NSInteger time = [[_modelOfData.data objectForKey:@"duration"] integerValue];
    
    NSInteger minute = time / 60;
    NSInteger second = time % 60;
    
    self.labelOfTime.text = [NSString stringWithFormat:@"时长: %02ld'%02ld''", minute, second];
    self.labelOfTime.textColor = [UIColor whiteColor];
    self.labelOfTime.font = [UIFont systemFontOfSize:13];
    
    self.labelOfInfo.text = [_modelOfData.data objectForKey:@"description"];
    self.labelOfInfo.textColor = [UIColor whiteColor];
    self.labelOfInfo.font = [UIFont systemFontOfSize:12];
    self.labelOfInfo.numberOfLines = 0;
    
    NSString *strOfLike = [NSString stringWithFormat:@"喜欢: %@", [[_modelOfData.data objectForKey:@"consumption"]objectForKey:@"collectionCount"]];
    [self.buttonOfLike setTitle:strOfLike forState:UIControlStateNormal];
    
    NSString *strOfShare = [NSString stringWithFormat:@"分享: %@", [[_modelOfData.data objectForKey:@"consumption"]objectForKey:@"shareCount"]];
    [self.buttonOfShare setTitle:strOfShare forState:UIControlStateNormal];
    
    NSString *strOfCount = [NSString stringWithFormat:@"回复: %@", [[_modelOfData.data objectForKey:@"consumption"]objectForKey:@"replyCount"]];
    [self.buttonOfCount setTitle:strOfCount forState:UIControlStateNormal];
    
    self.buttonOfLike.titleLabel.textColor = [UIColor whiteColor];
    self.buttonOfShare.titleLabel.textColor = [UIColor whiteColor];
    self.buttonOfCount.titleLabel.textColor = [UIColor whiteColor];
    
    self.buttonOfLike.titleLabel.font = [UIFont systemFontOfSize:13];
    self.buttonOfShare.titleLabel.font = [UIFont systemFontOfSize:13];
    self.buttonOfCount.titleLabel.font = [UIFont systemFontOfSize:13];
    
    self.imgOfPlayButton.image = [UIImage imageNamed:@"video-play"];
    
}

- (void)animationShow {
    
    self.imgOfBottom.frame = CGRectMake(0, self.offSetY + SCREEN_HEIGHT / 3 + 10, SCREEN_WIDTH, 0);
    self.imgOfTop.frame = CGRectMake(0, self.offSetY + SCREEN_HEIGHT / 3 + 10, SCREEN_WIDTH, 0);
    [UIView animateWithDuration:0.4 animations:^{
        
        self.imgOfTop.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.7);
        self.imgOfBottom.frame = CGRectMake(0, SCREEN_HEIGHT / 1.7, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_HEIGHT / 1.7);
        
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)animationDissmis {
    
    
    self.imgOfPlayButton.alpha = 0;
    self.buttonOfCount.alpha = 0;
    self.buttonOfLike.alpha = 0;
    self.buttonOfShare.alpha = 0;
    
    self.imgOfTop.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.7);
    self.imgOfBottom.frame = CGRectMake(0, SCREEN_HEIGHT / 1.7, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_HEIGHT / 1.7);
    
    [UIView animateWithDuration:0.4 animations:^{
        self.imgOfBottom.alpha = 0;
        self.imgOfTop.frame = CGRectMake(0, self.offSetY + SCREEN_HEIGHT / 3 + 10, SCREEN_WIDTH, 0);
        self.imgOfBottom.frame = CGRectMake(0, self.offSetY + SCREEN_HEIGHT / 3 + 10, SCREEN_WIDTH, 0);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

@end
